using System;
using Microsoft.Xna.Framework;

namespace BlobSim
{
    class CameraOrientation
    {
        private float _horizontalAngle;
        private float _verticalAngle;

        public CameraOrientation()
        {
            _horizontalAngle = MathHelper.ToRadians(90.0f);
            _verticalAngle = MathHelper.ToRadians(0.0f);
        }

        public float HorizontalAngle { get { return _horizontalAngle; }}
        public float VerticalAngle { get { return _verticalAngle; } }

        public void AdjustOrientation(Vector2 angleDelta, GameTime gameTime)
        {
            DoAdjustOrientation(angleDelta);
        }

        private void DoAdjustOrientation(Vector2 angleDelta)
        {
            _horizontalAngle += angleDelta.X;
            _verticalAngle += angleDelta.Y;
            const float verticalLimit = MathHelper.PiOver2 - 0.000001f;
            _verticalAngle = MathHelper.Clamp(_verticalAngle, -verticalLimit, verticalLimit);
        }

        public Vector3 ToVector()
        {
            var x = Math.Cos(_horizontalAngle) * Math.Cos(_verticalAngle);
            var y = Math.Sin(_verticalAngle);
            var z = -Math.Sin(_horizontalAngle) * Math.Cos(_verticalAngle);
            return new Vector3((float)x, (float)y, (float)z);
        }
    }
}
